package com.ruoyi.project.wx.service.impl;

import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.dao.MallShopUserDao;
import com.ruoyi.project.mall.domain.MallShopUserDO;
import com.ruoyi.project.wx.dao.*;
import com.ruoyi.project.wx.domain.*;
import com.ruoyi.project.wx.dto.FindGoodsDto;
import com.ruoyi.project.wx.dto.GoodsDto;
import com.ruoyi.project.wx.service.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;


@Service
public class GoodsGoodsServiceImpl implements GoodsGoodsService {
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private GoodsImagesService goodsImagesService;
	@Autowired
	private GoodsProductService goodsProductService;
	@Autowired
	private GoodsImagesDao goodsImagesDao;
	@Autowired
	private GoodsSpikeService goodsSpikeService;
	@Autowired
	private MemberFollowDao memberFollowDao;
	@Autowired
	private OrderComentDao orderComentDao;
	@Autowired
	private GoodsSpikeTimeDao goodsSpikeTimeDao;
	@Autowired
	private GoodsNineDao goodsNineDao;
	@Autowired
	private GoodsBargainDao goodsBargainDao;
	@Autowired
	private MemberCollectDao memberCollectDao;

	@Autowired
	private MallShopUserDao mallShopUserDao;
	@Autowired
	private GoodsClassDao goodsClassDao;
	/*@Autowired
	private ShopEstempService shopEstempService;*/
	/*@Autowired
    private OrderCommentService orderCommentService;
	@Autowired
    private ShopShippingService shopShippingService;*/
	
	@Override
	public GoodsGoodsDO get(String id){
		return goodsGoodsDao.get(id);
	}
	
	@Override
	public List<GoodsGoodsDO> list(Map<String, Object> map){
		return goodsGoodsDao.list(map);
	}

	@Override
	public List<Map<String, Object>> queryAllForIdAndName(Map<String, Object> map) {
		return goodsGoodsDao.queryAllForIdAndName(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return goodsGoodsDao.count(map);
	}
	
	@Override
	public int save(GoodsGoodsDO goodsGoods){
		return goodsGoodsDao.save(goodsGoods);
	}
	
	@Override
	public int update(GoodsGoodsDO goodsGoods){
		return goodsGoodsDao.update(goodsGoods);
	}
	
	@Override
	public int remove(String id){
		return goodsGoodsDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsGoodsDao.batchRemove(ids);
	}

	@Override
	public R findGoods(Map<String, Object> map) {
		PageHelperUtils.page(map);
		List<FindGoodsDto> goods = goodsGoodsDao.findByName(map.get("name").toString());
		/*if(map.containsKey("userId")&&StringUtils.isNotBlank(map.get("userId").toString())&&goods.size()>0){
			MemberUserDO memberUserDO = memberUserDao.get(map.get("userId").toString());
			for (FindGoodsDto findGoodsDto : goods) {
				if(memberUserDO.getLevel()=='A'){
					findGoodsDto.setPrice(findGoodsDto.getPriceA());
				}else if (memberUserDO.getLevel()=='B'){
					findGoodsDto.setPrice(findGoodsDto.getPriceB());
				}else if (memberUserDO.getLevel()=='C'){
					findGoodsDto.setPrice(findGoodsDto.getPriceC());
				}
			}
			
		}*/
		PageInfo<FindGoodsDto> pageInfo = new PageInfo<>(goods);
		return R.ok().put("data", pageInfo);
	}

	@Transactional(propagation= Propagation.REQUIRED)
	@Override
	public int add(GoodsGoodsDO goodsGoods, String products, String spec_values, String param_values, String images,
			String imagesDetail) {
		
		goodsGoods.setId(UuidUtil.get32UUID());
		goodsGoods.setParam(param_values);
		goodsGoods.setSpec(spec_values);
		goodsGoods.setCategory(0);
//		goodsGoods.setProp(prop_values);
		Date date = new Date();
		if (goodsGoods.getDisabled()==0) {//上架时间
			goodsGoods.setUpAt(date);
		}

		if(goodsGoods.getNumSale()==null){
			goodsGoods.setNumSale(0);
		}
		//设置参数
		goodsGoods.setGmtCreate(date);
		goodsGoods.setNumComment(0);
		goodsGoods.setNumView(0);
		goodsGoods.setNumSaleWeek(0);
		goodsGoods.setDelFlag(0);
		goodsGoods.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
		goodsGoods.setRecomSy(0);
		goodsGoods.setRecomXq(0);
		goodsGoods.setRecomGwc(0);
		goodsGoodsDao.save(goodsGoods);

		//商品图片
		List<Map<String,Object>> imgArr = (List<Map<String,Object>>)JSON.parse(images);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgArr != null) {
			String defaultImge = "";
			for (int i = 0; i < imgArr.size(); i++) {
				if (i == 0 || (boolean)imgArr.get(i).get("d")) defaultImge = imgArr.get(i).get("url").toString();
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(0);
				goodsImage.setImgurl(imgArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
				goodsImage.setDelFlag(0);
				goodsImagesService.save(goodsImage);
			}
			goodsGoods.setImgurl(defaultImge);
			goodsGoodsDao.update(goodsGoods);
		}

		//商品货品
		List<Map<String,Object>> productArr = (List<Map<String,Object>>) JSON.parse(products);
//		productArr
//		HashMap[] productArr = Json.fromJsonAsArray(HashMap.class, products);
		if (productArr != null) {
			if (goodsGoods.getHasSpec()==1 && StringUtils.isBlank(productArr.get(0).get("sku").toString())) {
				//如果没有启用规格，并且第一个货品SKU为null或空白字符则自动生成SKU
				productArr.get(0).put("sku", goodsProductService.getSkuPrefix());
			}
			for (int i = 0; i < productArr.size(); i++) {
				String name = "";
				String spec = productArr.get(i).get("spec").toString();
				GoodsProductDO product = new GoodsProductDO();
				product.setId(UuidUtil.get32UUID());
				product.setGoodsId(goodsGoods.getId());
				product.setLocation(i);
				product.setSpec(spec);
				product.setUnit(goodsGoods.getUnit());
				product.setIcon(goodsGoods.getImgurl());
				//如果SKU为null或空白字符则自动生成SKU
				if (StringUtils.isBlank(productArr.get(i).get("sku").toString())) {
					product.setSku(goodsProductService.getSkuPrefix() + "-" + i);
				} else {
					product.setSku(productArr.get(i).get("sku").toString().toUpperCase());
				}
				//通过规格获取名称
				StringBuffer bufName = new StringBuffer();
				if (spec.contains("*") && spec.contains(":")) {
					String[] str = StringUtils.split(spec, "*");
					for (String temp : str) {
						String[] s = StringUtils.split(temp, ":");
//						name += s[1] + " ";
						bufName.append(s[1] + " ");
					}
				} else if (spec.contains(":")) {
					String[] s = StringUtils.split(spec, ":");
//					name = s[1];
					bufName.append(s[1]);
				}
				name=bufName.toString();
				product.setName(name);
				Boolean isDefault = (boolean)productArr.get(i).get("isDefault");
				if (isDefault){
					product.setIsDefault(1);
				}else {
					product.setIsDefault(0);
				}
				Boolean disabled = (boolean)productArr.get(i).get("disabled");
				if (disabled){
					product.setDisabled(1);
				}else {
					product.setDisabled(0);
                    //上架时间
                    product.setUpAt(date);
				}
				String stock = productArr.get(i).get("stock").toString().replace(" ", "");
				product.setStock(NumberUtils.toInt(stock, 0));
				product.setBuyMin(NumberUtils.toInt(productArr.get(i).get("buyMin").toString().replace(" ", ""), 0));
				product.setBuyMax(NumberUtils.toInt(productArr.get(i).get("buyMax").toString().replace(" ", ""), 0));
				product.setWeight(NumberUtils.toInt(productArr.get(i).get("weight").toString().replace(" ", ""), 0));
				product.setPrice(new BigDecimal(productArr.get(i).get("price").toString()));
				if(productArr.get(i).get("postFee").toString()==null||"".equals(productArr.get(i).get("postFee").toString())){
					product.setPostFee(new BigDecimal("0.00"));
				}else{
					product.setPostFee(new BigDecimal(productArr.get(i).get("postFee").toString()));
				}
				product.setPriceA(new BigDecimal(productArr.get(i).get("priceA").toString()));
				product.setPriceB(new BigDecimal(productArr.get(i).get("priceB").toString()));
				product.setPriceC(new BigDecimal(productArr.get(i).get("priceC").toString()));
				//设置默认参数
				product.setNumSale(0);
				product.setLocation(i);
				product.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
				product.setGmtCreate(date);
				product.setDelFlag(0);
				goodsProductService.save(product);
				//会员价

			}
		}


		//商品货品
		List<Map<String,Object>> imgDetailArr = (List<Map<String,Object>>)JSON.parse(imagesDetail);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgDetailArr != null) {
			for (int i = 0; i < imgDetailArr.size(); i++) {
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(1);
				goodsImage.setImgurl(imgDetailArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
				goodsImage.setDelFlag(0);
				goodsImagesService.save(goodsImage);
			}
		}
		/*ShopEstempDO estemp = new ShopEstempDO();
		estemp.setAction("create");
		estemp.setGoodsId(goodsGoods.getId());
		estemp.setId(UuidUtil.get32UUID());
		estemp.setOpBy(getUserId());
		estemp.setOpAt(date);
		estemp.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		shopEstempService.save(estemp);*/
		return 1;
	
	}
	@Transactional(propagation= Propagation.REQUIRED)
	@Override
	public int edit(GoodsGoodsDO goodsGoods, String products, String spec_values, String param_values, String images,
			String imagesDetail) {
		if (spec_values.equals("[]") ) {
		} else {
			goodsGoods.setSpec(spec_values);
		}
		if (param_values.equals("[]") ) {
		} else {
			goodsGoods.setParam(param_values);
		}
		Date date = new Date();
		goodsGoods.setGmtModified(date);
		goodsGoods.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
		goodsGoodsDao.update(goodsGoods);
		//删除商品相册信息
		goodsImagesService.removeByGoodsId(goodsGoods.getId());
		//删除商品货品信息
		goodsProductService.removeByGoodsIdJ(goodsGoods.getId());
//		goodsProductService.removeByGoodsId(goodsGoods.getId());

		//商品图片
		List<Map<String,Object>> imgArr = (List<Map<String,Object>>)JSON.parse(images);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgArr != null) {
			String defaultImge = "";
			for (int i = 0; i < imgArr.size(); i++) {
				if (i == 0 || (boolean)imgArr.get(i).get("d")) defaultImge = imgArr.get(i).get("url").toString();
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(0);
				goodsImage.setImgurl(imgArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
				goodsImage.setDelFlag(0);
				goodsImagesService.save(goodsImage);
			}
			goodsGoods.setImgurl(defaultImge);
			goodsGoodsDao.update(goodsGoods);
		}

		//商品货品
		List<Map<String,Object>> productArr = (List<Map<String,Object>>) JSON.parse(products);
//		productArr
//		HashMap[] productArr = Json.fromJsonAsArray(HashMap.class, products);
		if (productArr != null) {
			if (goodsGoods.getHasSpec()==1 && StringUtils.isBlank(productArr.get(0).get("sku").toString())) {
				//如果没有启用规格，并且第一个货品SKU为null或空白字符则自动生成SKU
				productArr.get(0).put("sku", goodsProductService.getSkuPrefix());
			}
			for (int i = 0; i < productArr.size(); i++) {
				String productId = "";
				String name = "";
				String spec = productArr.get(i).get("spec").toString();
				GoodsProductDO product = new GoodsProductDO();
				if( productArr.get(i).containsKey("productId")){
					
						productId = productArr.get(i).get("productId").toString();
					
				}
				
				product.setGoodsId(goodsGoods.getId());
				product.setLocation(i);
				product.setSpec(spec);
				product.setUnit(goodsGoods.getUnit());
				product.setIcon(goodsGoods.getImgurl());
				//如果SKU为null或空白字符则自动生成SKU
				if (StringUtils.isBlank(productArr.get(i).get("sku").toString())) {
					product.setSku(goodsProductService.getSkuPrefix() + "-" + i);
				} else {
					product.setSku(productArr.get(i).get("sku").toString().toUpperCase());
					Map<String, Object> skuMap = new HashMap<>();
					skuMap.put("sku", product.getSku());
					List<GoodsProductDO> list = goodsProductService.list(skuMap);
					if(list.size()>0){
						productId=list.get(0).getId();
					}
				}
				//通过规格获取名称
				StringBuffer bufName = new StringBuffer();
				if (spec.contains("*") && spec.contains(":")) {
					String[] str = StringUtils.split(spec, "*");
					for (String temp : str) {
						String[] s = StringUtils.split(temp, ":");
//						name += s[1] + " ";
						bufName.append(s[1] + " ");
					}
				} else if (spec.contains(":")) {
					String[] s = StringUtils.split(spec, ":");
//					name = s[1];
					bufName.append(s[1]);
				}
				name=bufName.toString();
				product.setName(name);
				Boolean isDefault = (boolean)productArr.get(i).get("isDefault");
				if (isDefault){
					product.setIsDefault(1);
				}else {
					product.setIsDefault(0);
				}
				Boolean disabled = (boolean)productArr.get(i).get("disabled");
				if (disabled){
					product.setDisabled(1);
				}else {
					product.setDisabled(0);
					//上架时间
					product.setUpAt(date);
				}
				product.setStock(NumberUtils.toInt(productArr.get(i).get("stock").toString().replaceAll(" ", ""), 0));
				product.setBuyMin(NumberUtils.toInt(productArr.get(i).get("buyMin").toString().replaceAll(" ", ""), 1));
				product.setBuyMax(NumberUtils.toInt(productArr.get(i).get("buyMax").toString().replaceAll(" ", ""), 100));
				product.setWeight(NumberUtils.toInt(productArr.get(i).get("weight").toString().replaceAll(" ", ""), 0));
				product.setPrice(new BigDecimal(productArr.get(i).get("price").toString()));
				if(productArr.get(i).get("postFee").toString()==null||"".equals(productArr.get(i).get("postFee").toString())){
					product.setPostFee(new BigDecimal("0.00"));
				}else{
					product.setPostFee(new BigDecimal(productArr.get(i).get("postFee").toString()));
				}
				
				product.setPriceA(new BigDecimal(productArr.get(i).get("priceA").toString()));
				product.setPriceB(new BigDecimal(productArr.get(i).get("priceB").toString()));
				product.setPriceC(new BigDecimal(productArr.get(i).get("priceC").toString()));
//				product.setRebateTwo(new BigDecimal(productArr.get(i).get("rebateTwo").toString()));
				//设置默认参数
				product.setNumSale(0);
				product.setLocation(i);
				product.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
				product.setGmtCreate(date);
				product.setDelFlag(0);
				if (StringUtils.isBlank(productId)){
					product.setId(UuidUtil.get32UUID());
					goodsProductService.save(product);
				}else {
					product.setId(productId);
					int update = goodsProductService.update(product);
					System.out.println(update);
				}
				
				//会员价
			}
		}

		//商品货品
		List<Map<String,Object>> imgDetailArr = (List<Map<String,Object>>)JSON.parse(imagesDetail);
		//商品相册
//		HashMap<String,Object>[] imgArr = Json.fromJsonAsArray(NutMap.class, images);
		if (imgDetailArr != null) {
			for (int i = 0; i < imgDetailArr.size(); i++) {
				GoodsImagesDO goodsImage = new GoodsImagesDO();
				goodsImage.setGoodsId(goodsGoods.getId());
				goodsImage.setLocation(i);
				goodsImage.setAddress(1);
				goodsImage.setImgurl(imgDetailArr.get(i).get("url").toString());
				goodsImage.setId(UuidUtil.get32UUID());
				goodsImage.setGmtCreate(date);
				goodsImage.setUserIdCreate(SecurityUtils.getLoginUser().getUser().getUserId());
				goodsImage.setDelFlag(0);
				goodsImagesService.save(goodsImage);
			}
		}
		/*ShopEstempDO estemp = new ShopEstempDO();
		estemp.setAction("update");
		estemp.setGoodsId(goodsGoods.getId());
		estemp.setId(UUIDUtils.getUUID());
		estemp.setOpBy(getUserId());
		estemp.setOpAt(date);
		estemp.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		shopEstempService.save(estemp);*/
		return 1;
	}

	/**
	 * 根据分类查询商品列表
	 */
	@Override
	public R goodsListByClass(Map<String, Object> params) {

		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList =  goodsGoodsDao.goodsListByClass(params);
		//changePrice(params, goodsList);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data", pageInfo);
	}
	
	
	/**
	 * 普通商品详情页
	 */
	@Override
	public R getDetail(Map<String, Object> params) {
		String goodsId = params.get("goodsId").toString();
		String userId = params.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);

		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsGoodsDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(goodsGoodsDO.getCategory());
		goodsDto.setAfterSale(goodsGoodsDO.getAfterSale());

//		equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsProductDO.getStock());
				goodsDto.setPostFee(goodsProductDO.getPostFee());
				/*if(memberUserDO.getLevel()=='A'){
					goodsDto.setPrice(goodsProductDO.getPriceA());
				}else if (memberUserDO.getLevel()=='B'){
					goodsDto.setPrice(goodsProductDO.getPriceB());
				}else if (memberUserDO.getLevel()=='C'){
					goodsDto.setPrice(goodsProductDO.getPriceC());
				}else{*/
					goodsDto.setPrice(goodsProductDO.getPrice());
				//}

			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			map.put("postFee", goodsProductDO.getPostFee());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			map.put("price", goodsProductDO.getPrice());
			//returnPrice1(memberUserDO, goodsProductDO, map);

			productList.add(map);
		}
		goodsDto.setProductList(productList);
		/*//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}*/
		//是否收藏
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("collectId",goodsId);
		searchMap.put("userId",userId);
		searchMap.put("status","0");
		List list = memberCollectDao.list(searchMap);
		if (list.size() <= 0) {
			goodsDto.setCollect(false);
		} else {
			goodsDto.setCollect(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);

		Map map = KitUtil.toMap(goodsDto);
		//商家信息
		MallShopUserDO mallShopUserDO = mallShopUserDao.get(goodsGoodsDO.getShopId());
		if(mallShopUserDO!=null){
			map.put("shopName",mallShopUserDO.getName());
			map.put("shopLogo",mallShopUserDO.getLogo());
			map.put("shopAddress",mallShopUserDO.getProvince()+"  "+mallShopUserDO.getCity()+"  "+mallShopUserDO.getArea()+"  "+mallShopUserDO.getAddress());
			map.put("shopIntro",mallShopUserDO.getIntro());
			map.put("shopCustomerMobile",mallShopUserDO.getMobile());
			map.put("shopId",mallShopUserDO.getId());
			map.put("shopUserId",mallShopUserDO.getUserId());
			map.put("shopTag",mallShopUserDO.getTag());
			map.put("serverId",mallShopUserDO.getServerId());
		}else{
			map.put("shopName","");
			map.put("shopLogo","");
			map.put("shopAddress","");
			map.put("shopIntro","");
			map.put("shopCustomerMobile","");
			map.put("shopId","");
			map.put("shopUserId","");
			map.put("shopTag","1");
			map.put("serverId","");
		}


		return R.ok().put("data", map);
	}

	private void returnPrice1(MemberUserDO memberUserDO, GoodsProductDO goodsProductDO, Map<String, Object> map) {
		/*if(memberUserDO.getLevel()=='A'){
			map.put("price", goodsProductDO.getPriceA());
		}else if (memberUserDO.getLevel()=='B'){
			map.put("price", goodsProductDO.getPriceB());
		}else if (memberUserDO.getLevel()=='C'){
			map.put("price", goodsProductDO.getPriceC());
		}else{*/
			map.put("price", goodsProductDO.getPrice());
		//}
	}

	private void returnPrice(MemberUserDO memberUserDO, GoodsDto goodsDto, Price goodsProductDO) {
		/*if(memberUserDO.getLevel()=='A'){
			goodsDto.setSpikePrice(goodsProductDO.getPriceA());
		}else if (memberUserDO.getLevel()=='B'){
			goodsDto.setSpikePrice(goodsProductDO.getPriceB());
		}else if (memberUserDO.getLevel()=='C'){
			goodsDto.setSpikePrice(goodsProductDO.getPriceC());
		}else{*/
			goodsDto.setSpikePrice(goodsProductDO.getPrice());
		//}
	}

	/**
	 * 秒杀商品详情页
	 */
	@Override
	public R getSpikeDetail(Map<String, Object> params) {
		String spikeId = params.get("spikeId").toString();
		GoodsSpikeDO goodsSpikeDO = goodsSpikeService.get(spikeId);
		String goodsId = goodsSpikeDO.getGoodsId();
		params.put("goodsId", goodsId);
		GoodsSpikeTimeDO goodsSpikeTimeDO = goodsSpikeTimeDao.get(goodsSpikeDO.getTimeId());
		String userId = params.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(spikeId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsSpikeDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(1);
		goodsDto.setStartTime(goodsSpikeTimeDO.getStartAt());
		goodsDto.setEndTime(goodsSpikeTimeDO.getEndAt());

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsSpikeDO.getStock());
				goodsDto.setPrice(goodsProductDO.getPrice());
				goodsDto.setPostFee(goodsProductDO.getPostFee());
				returnPrice(memberUserDO,goodsDto,goodsSpikeDO);
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			returnPrice1(memberUserDO, goodsProductDO, map);
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}

	/**
	 * 9.9专区商品详情页
	 */
	@Override
	public R getNineDetail(Map<String, Object> params) {
		String nineId = params.get("nineId").toString();
		String userId = params.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		GoodsNineDO goodsNineDO = goodsNineDao.get(nineId);
		String goodsId = goodsNineDO.getGoodsId();
		params.put("goodsId", goodsId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(nineId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsNineDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(2);

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsNineDO.getStock());
				goodsDto.setPrice(goodsProductDO.getPrice());
				goodsDto.setPostFee(goodsProductDO.getPostFee());
				returnPrice(memberUserDO,goodsDto, (Price) goodsNineDO);
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			returnPrice1(memberUserDO, goodsProductDO, map);
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}

	/**
	 * 砍价商品详情页
	 */
	@Override
	public R getBargainDetail(Map<String, Object> params) {
		String bargainId = params.get("bargainId").toString();
		String userId = params.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		GoodsBargainDO goodsBargainDO = goodsBargainDao.get(bargainId);
		String goodsId = goodsBargainDO.getGoodsId();
		params.put("goodsId",goodsId);
		GoodsGoodsDO  goodsGoodsDO=goodsGoodsDao.getDetail(params);
		List<GoodsProductDO> goodsProductList = goodsProductDao.getGoodsByGoodsId(params);
		goodsGoodsDO.setGoodsProductList(goodsProductList);
		List<GoodsImagesDO> goodsImagesList = goodsImagesDao.getGoodsImgs(params);
		GoodsDto goodsDto = new GoodsDto();
		goodsDto.setId(bargainId);
		goodsDto.setGoodsId(goodsGoodsDO.getId());
		goodsDto.setName(goodsGoodsDO.getName());
		goodsDto.setNote(goodsGoodsDO.getNote());
		goodsDto.setNumSale(goodsBargainDO.getNumSale());
		goodsDto.setParam(goodsGoodsDO.getParam());
		goodsDto.setSpec(goodsGoodsDO.getSpec());
		goodsDto.setTitle(goodsGoodsDO.getTitle());
		goodsDto.setImgurl(goodsGoodsDO.getImgurl());
		goodsDto.setCategory(3);

//		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			if (goodsProductDO.getIsDefault()==1) {
				goodsDto.setStock(goodsBargainDO.getStock());
				goodsDto.setPrice(goodsProductDO.getPrice());
				goodsDto.setPostFee(goodsProductDO.getPostFee());
				returnPrice(memberUserDO,goodsDto,goodsBargainDO);
			}
		}

		List<String> bannerList = new ArrayList<>();
		List<String> goodsImgList = new ArrayList<>();

		for (GoodsImagesDO goodsImagesDO : goodsImagesList) {
			if (goodsImagesDO.getAddress()==0) {
				bannerList.add(goodsImagesDO.getImgurl());
			}else if(goodsImagesDO.getAddress()==1){
				goodsImgList.add(goodsImagesDO.getImgurl());
			}
		}
		goodsDto.setBanner(bannerList);
		goodsDto.setGoodsImg(goodsImgList);

		//货品信息
		List<Map<String, Object>> productList = new ArrayList<>();
//		if("1".equals(params.get("spikeId").toString())){
		for (GoodsProductDO goodsProductDO : goodsProductList) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", goodsProductDO.getName());
			map.put("icon", goodsProductDO.getIcon());
			map.put("spec", goodsProductDO.getSpec());
			//map.put("price_market", goodsProductDO.getPriceMarket());
			map.put("stock", goodsProductDO.getStock());
			map.put("unit", goodsProductDO.getUnit());
			map.put("numNale", goodsProductDO.getNumSale());
			map.put("id", goodsProductDO.getId());
			returnPrice1(memberUserDO, goodsProductDO, map);
			productList.add(map);
		}
		goodsDto.setProductList(productList);
		//是否关注
		Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("goodsId",goodsId);
		searchMap.put("userId",userId);
		List list = memberFollowDao.list(searchMap);
		if (list.size() == 0) {
			goodsDto.setisFollow(false);
		} else {
			goodsDto.setisFollow(true);
		}
		//查询评论个数
		int comentSize = orderComentDao.getComentByGoods(params).size();
		goodsDto.setComentSize(comentSize);
		return R.ok().put("data", goodsDto);
	}



	private void removePrice(Map<String, Object> map) {
		map.remove("price_f");
		map.remove("price_l");
		map.remove("price_h");

	}

	/*@Override
	public R getGroupGoods(Map<String, Object> params) {
		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList = goodsGoodsDao.getGroupGoods(params);
		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
		for (Map<String, Object> map : goodsList) {
		
			if(memberUserDO.getLevel()==0){
				map.put("price", map.get("price_f"));
			}else if (memberUserDO.getLevel()==1){
				map.put("price", map.get("price_h"));
			}else if (memberUserDO.getLevel()==2){
				map.put("price", map.get("price_l"));
			}
			removePrice(map);
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		
		return R.ok().put("data", pageInfo);
	}*/

	/*@Override
	public R getSpike(Map<String, Object> params) {
		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList  = goodsGoodsDao.getSpike();
		MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
		for (Map<String, Object> map : goodsList) {
		
			if(memberUserDO.getLevel()==0){
				map.put("price", map.get("price_f"));
			}else if (memberUserDO.getLevel()==1){
				map.put("price", map.get("price_h"));
			}else if (memberUserDO.getLevel()==2){
				map.put("price", map.get("price_l"));
			}
			removePrice(map);
		}
		
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data", pageInfo);
	}*/

	/**
	 * 推荐商品列表
	 */
	@Override
	public R recommend(Map<String, Object> params) {

		PageHelperUtils.page(params);
		List<Map<String, Object>> goodsList = goodsGoodsDao.recommend(params);
		//changePrice(params, goodsList);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);

		return R.ok().put("data", pageInfo);
	}

	private void changePrice(Map<String, Object> params, List<Map<String, Object>> goodsList) {
		if(params.containsKey("userId")&&StringUtils.isNotBlank(params.get("userId").toString())&&goodsList.size()>0){
			MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
			for (Map<String, Object> map : goodsList) {
				if(memberUserDO.getLevel()=='A'){
					map.put("price", new BigDecimal(map.get("price_a").toString()));
				}else if (memberUserDO.getLevel()=='B'){
					map.put("price", new BigDecimal(map.get("price_b").toString()));
				}else if (memberUserDO.getLevel()=='C'){
					map.put("price", new BigDecimal(map.get("price_c").toString()));
				}
			}
			
		}
	}

	@Override
	public List<Map<String, Object>> getGoodsOrder(Map<String, Object> params) {
		
		return goodsGoodsDao.getGoodsOrder(params);
	}

	
	
	
}
